import { addHobby, query, del,getToDoList } from '../service';
import moment from 'moment';
const creator = (type, payload) => ({ type, payload });
const handle=(type,arr1,arr2)=>{
  for(let i=0;i<arr1.length;i++){
    let value=0;
    for(let j=0;j<arr2.length;j++){
      if(arr1[i].hobby===arr2[j].type){
        value++
      }
    }
    arr1[i][type]=value
  }
  return arr1
}
export default {
  namespace: 'hobby',
  state: {
    hobbies: [],
    handleHobbies:[]
  },
  effects: {
    //获取列表
    *add({ payload }, { call, put }) {
      const res = yield call(addHobby, payload);
      return res;
    },
    *query({ payload }, { call, put }) {
      const res = yield call(query, payload);
      if (res.httpStatus === 200) {
        yield put(creator('save', { hobbies: res.data }));
      }
    },
    *getHobbyAndValue({ payload }, { call, put }) {
      const titleRes = yield call(query, payload.id);
      const allListRes=yield call(getToDoList,{id:payload.id,sDate:payload.yStart,eDate:payload.yEnd})
      const weekListRes=yield call(getToDoList,{id:payload.id,sDate:payload.wStart,eDate:payload.wEnd})
      const titleData=titleRes.data;
      const allData=allListRes.data;
      const weekData=weekListRes.data;
     

      let data=handle('total',titleData,allData)
      let res=handle('week',data,weekData)
      yield put(creator('save',{handleHobbies:res}))
    },
    
    *del({ payload }, { call, put }) {
      const res = yield call(del, payload);
      return res
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
